package com.vincce.fitness.core.config;

import com.vincce.fitness.core.base.AuthRealm;
import net.sf.ehcache.config.CacheConfiguration;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by henry on 4/18/14.
 */
@Configuration
public class AuthConfig {

	@Bean
	public SecurityManager securityManager(Realm shiroDbRealm, CacheManager shiroEhcacheManager) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(shiroDbRealm);
		securityManager.setCacheManager(shiroEhcacheManager);
		return securityManager;
	}

	@Bean
	public EhCacheManager shiroEhcacheManager() {
        net.sf.ehcache.config.Configuration managerConfiguration = new net.sf.ehcache.config.Configuration()
                .name("shiroCache").updateCheck(false)
                .defaultCache(new CacheConfiguration()
                                .maxElementsInMemory(10000)
                                .eternal(true)
                                .timeToIdleSeconds(120)
                                .timeToLiveSeconds(120)
                                .overflowToDisk(false)
                                .diskPersistent(false)
                                .diskExpiryThreadIntervalSeconds(120)
                );
        net.sf.ehcache.CacheManager manager = net.sf.ehcache.CacheManager.create(managerConfiguration);
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManager(manager);
        return cacheManager;
	}

	@Bean
	public Realm shiroDbRealm() {
		AuthRealm authRealm = new AuthRealm();
		authRealm.setCredentialsMatcher(new HashedCredentialsMatcher("MD5"));
		authRealm.setAuthorizationCachingEnabled(true);
		return authRealm;
	}

	@Bean
	public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		shiroFilterFactoryBean.setLoginUrl("/login.htm");
		shiroFilterFactoryBean.setSuccessUrl("/home.htm");
		shiroFilterFactoryBean.setUnauthorizedUrl("/login.htm");
		Map<String, Filter> filters = new HashMap<String, Filter>();
		filters.put("formFilter", new FormAuthenticationFilter());
		shiroFilterFactoryBean.setFilters(filters);
		Map<String, String> filterChainDefinitionMap = new HashMap<String, String>();
		filterChainDefinitionMap.put("/logout.htm", "logout");
		filterChainDefinitionMap.put("/**", "formFilter");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return shiroFilterFactoryBean;
	}

	@Bean
	@DependsOn("lifecycleBeanPostProcessor")
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
		defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
		return defaultAdvisorAutoProxyCreator;
	}

	@Bean
	public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
		AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
		authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
		return authorizationAttributeSourceAdvisor;
	}

}
